package com.g7.dsp.fortest.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.poi.POIXMLException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.g7.dsp.forprotocol.contexts.ApplicationContext;

public class ConvertGui implements ActionListener {

	ApplicationContext app;
	JTextField ja1 = new JTextField(20);
	FileDialog fd = null;
	JFrame jf = null;
	JButton jb1 =null;
	JButton jb2 = null;
	JButton jb3 = null;
	String oldfilename;
	JPanel j3 = null;
	boolean isExcelToXml=false;
	JComboBox<String> comboBox=new JComboBox<String>();
    public ConvertGui(JFrame jframe){
    	comboBox.setPreferredSize(new Dimension(300,20));
    	this.app = ApplicationContext.getApplicationContext();
		this.jf = jframe;
		this.fd = new FileDialog(this.jf);
    	JPanel j1 = new JPanel();
    	j3 = new JPanel();
    	JPanel j2 = new JPanel();	
    	j3.setVisible(false);
    	JLabel jl1 = new JLabel("源文件:");
    	JLabel jl2 = new JLabel("sheet:");
    	jb1 = new JButton("选择");
    	jb2 = new JButton("xml转成excel");
    	jb3 = new JButton("excel转成xml");  
    	jb1.addActionListener(this);
    	jb2.addActionListener(this);
    	jb3.addActionListener(this);
    	j1.add(jl1);
    	ja1.setEditable(false);
    	ja1.setBackground(Color.white);
    	j1.add(ja1);
    	j1.add(jb1);   	
    	j3.add(jl2);
    	j3.add(comboBox);
    	jb2.setEnabled(false);
    	jb3.setEnabled(false);
    	j2.add(jb2);
    	j2.add(jb3);
    	jf.add(j1,BorderLayout.NORTH);
    	jf.add(j3);
    	jf.add(j2,BorderLayout.SOUTH);
    	app.initUI(jf);
		app.getPanelList().add(j1);
		app.getPanelList().add(j3);
		app.getPanelList().add(j2);
		jf.setSize(400, 400);
    	jf.setVisible(true);
//    	jf.pack();
//    	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
	public void actionPerformed(ActionEvent action) {
		String comm = action.getActionCommand();
		if(comm.equals("选择")){
			fd.setVisible(true);
			String file = fd.getFile();
			if(file!=null){
				if(file.endsWith(".xml")||file.endsWith(".xls")
						||file.endsWith(".xlsx")){
					ja1.setText(fd.getDirectory()+fd.getFile());
					oldfilename=fd.getDirectory()+fd.getFile();
					if(fd.getFile().endsWith(".xml")){
						jb2.setEnabled(true);
						jb3.setEnabled(false);
					}else{
						comboBox.removeAllItems();
						// 默认创建2007版本的Excel文件对象
				        XSSFWorkbook xswb = null;
				        //出现异常时，创建2003版本的Excel文件对象
				        HSSFWorkbook hswb = null;
				        try {  
				            xswb = new XSSFWorkbook(new FileInputStream(new File(oldfilename)));
				            // 创建对工作表的引用
				            for (int i = 0; i < xswb.getNumberOfSheets();i++) {
				            	comboBox.addItem(xswb.getSheetName(i));
				            } 
				        }
				        catch (FileNotFoundException e) {  
				        	e.printStackTrace(); 
				        } catch (POIXMLException e) {  
							try {
								hswb = new HSSFWorkbook(new FileInputStream(new File(oldfilename)));
								for (int i = 0; i < hswb.getNumberOfSheets();i++) {
					            	comboBox.addItem(hswb.getSheetName(i));
								}  
							}
							catch (FileNotFoundException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} 
							catch (NullPointerException e1) {
								// TODO Auto-generated catch block
								System.out.println("In 2003 excel,TestCase and TestCase between can't have empty row.");
								System.out.println("OR some columns or some rows are not in border.");
								e1.printStackTrace();
							} 
							catch (Exception e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
				        }
				        catch (IOException e) {  
				        	e.printStackTrace();   
				        } catch (NullPointerException e1) {
							// TODO Auto-generated catch block
							System.out.println("In 2007 excel,TestCase and TestCase between can't have empty row.");
							System.out.println("OR some columns or some rows are not in border.");
							e1.printStackTrace();
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
				        j3.setVisible(true);			        
						jb3.setEnabled(true);
						jb2.setEnabled(false);
						jf.pack();
					}
					
				}else{
					JOptionPane.showMessageDialog(ja1,"请重新选择xml或excel文件！");
					comboBox.removeAllItems();
					j3.setVisible(false);				
					ja1.setText("");
					jb2.setEnabled(false);
					jb3.setEnabled(false);
					jf.pack();
				}
			}
		}else if(comm.equals("xml转成excel")){
			isExcelToXml=false;
			j3.setVisible(false);
			jb2.setEnabled(false);
			jb1.setEnabled(false);
			jf.pack();
			new FileTransferTool(ja1,jb1,jb2,jb3,oldfilename,isExcelToXml,String.valueOf(comboBox.getSelectedItem())).start();			
		}else{
			isExcelToXml=true;
			j3.setVisible(false);
			jb3.setEnabled(false);
			jb1.setEnabled(false);
			jf.pack();
			new FileTransferTool(ja1,jb1,jb2,jb3,oldfilename,isExcelToXml,String.valueOf(comboBox.getSelectedItem())).start();			
		}
	}
}
